package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ListViewActivity extends AppCompatActivity {

    ListView myList = null;
    private ImageView iv = null;
    private TextView title, price;

//    1. 定义适配器中的数组内容
//    图片资源
    private int[] icons = {R.drawable.girl1, R.drawable.girl2, R.drawable.girl3,
        R.drawable.girl4, R.drawable.girl5, R.drawable.girl6, R.drawable.girl7,
        R.drawable.girl8, R.drawable.girl9, R.drawable.girl10};
//    标题资源
    private String[] titles = {"girl1", "girl2", "girl3", "girl4", "girl5",
            "girl6", "girl7", "girl8", "girl9", "girl10", };
//    价格资源
    private String[] prices = {"100元", "100元", "100元", "100元", "100元",
        "100元", "100元", "100元", "100元", "100元", };

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

        myList = findViewById(R.id.myList);

//        适配器
        MyAdapter md = new MyAdapter();
//        添加适配器
        myList.setAdapter(md);
    }

//    适配器内部类
    private class MyAdapter extends BaseAdapter {
        @Override
    //    获取item的数量
        public int getCount() {
            return icons.length;
        }

        @Override
    //    i接收当前item的序号（从0开始），getItem获取用户点击的Item对象
        public Object getItem(int i) {
            return icons[i];
        }

        @Override
    //    获取到选中的item项的编号
        public long getItemId(int i) {
            return i;
        }

        @Override
    //    配置listview要加载的内容，包括视图和数据
    //    i表示item的序号，view是用于缓存item视图的对象
        public View getView(int i, View view, ViewGroup viewGroup) {
            /*
            * 方案一：一次性显示出所有item
            * //        将item的xml布局文件解析成listview对象
            View listview = View.inflate(ListViewActivity.this, R.layout.list_view_layout, null);

    //        将xml布局文件的控件赋值给对应的变量
            iv = listview.findViewById(R.id.iv);
            title = listview.findViewById(R.id.title);
            price = listview.findViewById(R.id.price);

    //        内容赋值
            iv.setBackgroundResource(icons[i]);
            title.setText(titles[i]);
            price.setText(prices[i]);

            return listview;
            *
            *
            *
            * */

    //        改进方案
            ViewHolder holder = null;
//            view是用于缓存item视图的对象
            if (view == null){  // 无对象缓存
//                将item的xml布局文件解析成listview对象
                view = View.inflate(ListViewActivity.this, R.layout.list_view_layout, null);
//                找到xml中创建的标签
                holder = new ViewHolder();
                holder.iv = (ImageView) view.findViewById(R.id.iv);
                holder.title = (TextView) view.findViewById(R.id.title);
                holder.price = (TextView) view.findViewById(R.id.price);
//                将标签装载到view中
                view.setTag(holder);
            } else {  // 有缓存则加载holder
                holder = (ViewHolder) view.getTag();
            }
            //        内容赋值
            holder.iv.setBackgroundResource(icons[i]);
            holder.title.setText(titles[i]);
            holder.price.setText(prices[i]);

            return view;
        }
    }
//    改进方案的静态类
    static class ViewHolder{
        ImageView iv;
        TextView title;
        TextView price;
    }

}